#include "sht20.h"

/**
  * @brief  soft reset by transmit reset command
  * @retval None
  */
void _shtxx_Init(SHTXX *shtxx)
{
  shtxx->iic.Start(&shtxx->iic);
  shtxx->iic.WriteByte(&shtxx->iic,SHT20ADDR & 0xfe);  //shtxx->iic. address + write
  shtxx->iic.WriteByte(&shtxx->iic,SOFT_RESET);        //soft reset
  shtxx->iic.Stop(&shtxx->iic);
}

/**
  * @brief  SET HTU20D resolution by write register
  * @retval None
  */
void _shtxx_SET_Resolution(SHTXX *shtxx)
{
    shtxx->iic.Start(&shtxx->iic);
    shtxx->iic.WriteByte(&shtxx->iic,SHT20ADDR & 0xfe) ;
    shtxx->iic.WaitAck(&shtxx->iic); //shtxx->iic. address + write + ACK

    shtxx->iic.WriteByte(&shtxx->iic,WRITE_REGISTER);
    shtxx->iic.WaitAck(&shtxx->iic);  //写用户寄存器

    shtxx->iic.WriteByte(&shtxx->iic,0x83);
    shtxx->iic.WaitAck(&shtxx->iic);          //设置分辨率    11bit RH% 测量时间：12ms(typ.)
    //             11bit T℃ 测量时间： 9ms(typ.)

    shtxx->iic.Stop(&shtxx->iic);
}

/**
  * @brief  read sht20's converted result
  * @param  TempOrHumiCMD: operation command for temperature or humidity
  * @retval None
  */
float _shtxx_Read(SHTXX *shtxx ,uint8_t TempOrHumiCMD)
{
  float temp;
  uint8_t MSB,LSB;
  uint8_t retry = 0;

  shtxx->SET_Resolution(shtxx);
  shtxx->iic.Start(&shtxx->iic);
  shtxx->iic.WriteByte(&shtxx->iic,SHT20ADDR & 0xfe);
  shtxx->iic.WaitAck(&shtxx->iic);      //shtxx->iic. address + write + ACK

    shtxx->iic.WriteByte(&shtxx->iic,TempOrHumiCMD); 
    shtxx->iic.WaitAck(&shtxx->iic);       //command
    do{
       shtxx->iic.delay_nus(500);
       shtxx->iic.Start(&shtxx->iic);
       shtxx->iic.WriteByte(&shtxx->iic,SHT20ADDR | 0x01);
       retry++;
    }
   while(shtxx->iic.WaitAck(&shtxx->iic) == 0 && retry < 200); 
    //   == NACK); //shtxx->iic. address + read + NACK
    if(retry <200) shtxx->statu = 1;
    else shtxx->statu = 0;
    MSB = shtxx->iic.ReadByte(&shtxx->iic);
    shtxx->iic.Ack(&shtxx->iic);
    LSB = shtxx->iic.ReadByte(&shtxx->iic);
    shtxx->iic.Ack(&shtxx->iic);
    shtxx->iic.ReadByte(&shtxx->iic);                        //Checksum  + NACK
    shtxx->iic.NoAck(&shtxx->iic);
    shtxx->iic.Stop(&shtxx->iic);
    LSB &= 0xfc;                                //Data (LSB) 的后两位在进行物理计算前前须置0
    temp = (MSB << 8) + LSB;
    
  return temp;
}
void _shtxx_ReadTemp(SHTXX *shtxx)
{
    shtxx->Temperature = shtxx->Read(shtxx,TRIGGER_TEMP);
    shtxx->Temperature = (shtxx->Temperature * 175.72) / 65536 - 46.85;
}
void _shtxx_ReadHumi(SHTXX *shtxx)
{
    shtxx->Humidity = shtxx->Read(shtxx,TRIGGER_HUMI);
    shtxx->Humidity = (shtxx->Humidity * 125) / 65536 - 6;
}


SHTXX new_shtxx(uint32_t scl_gpiox,uint32_t scl_gpio_pinx,uint32_t sda_gpiox,uint32_t sda_gpio_pinx)
{
	SHTXX temp;
	temp.iic = new_soft_iic(scl_gpiox,scl_gpio_pinx,sda_gpiox,sda_gpio_pinx);
	temp.Init = _shtxx_Init;
	temp.SET_Resolution = _shtxx_SET_Resolution;
	temp.Read = _shtxx_Read;
	temp.ReadTemp = _shtxx_ReadTemp;
	temp.ReadHumi = _shtxx_ReadHumi;
	temp.Init(&temp);
	return temp;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

#ifdef CMSIS_OS
#include "board.h"
#include "log.h"
osThreadId Shtxx_thread_Handle;
void Shtxx_thread(void const * argument)
{
    SHTXX *sht20 = (SHTXX*)argument;
    while(1)
    {
          sht20->ReadTemp(sht20);
          log_info("sht20 temp is %0.3f",sht20->Temperature);
          sht20->ReadHumi(sht20);
          log_info("sht20 humi is %0.3f",sht20->Humidity);
<<<<<<< HEAD
          osDelay(3000);
=======
          osDelay(looptime1);
>>>>>>> ae23b613e238cc4e21873d92e60135d5cd0e67e7
    }
}

osThreadId Create_Shtxx_thread(SHTXX *arg)
{
    /* definition and creation of defaultTask */
  osThreadDef(shtxx_thread, Shtxx_thread, osPriorityIdle, 0, 128);
  Shtxx_thread_Handle = osThreadCreate(osThread(shtxx_thread),(void*)arg);
  if (Shtxx_thread_Handle == NULL)log_info("shtxx_thread create fail");
  else log_info("shtxx_thread create OK");
  return Shtxx_thread_Handle;
}

#endif



